var e = require("../../@babel/runtime/helpers/interopRequireDefault")(require("../../@babel/runtime/regenerator")), t = require("../../@babel/runtime/helpers/asyncToGenerator"), r = require("../../@babel/runtime/helpers/defineProperty"), a = getApp(), s = require("../../utils/timeUtil.js"), i = require("../../http/activityApi.js");

Page({
    distTime: 0,
    shareDone: !0,
    data: {},
    onLoad: function(e) {
        if (e.shareFrom && e.usedId) {
            if (!a.isLogin()) return void a.loginThenRedirectTo(!0, this.buildSharePath(e.shareFrom, e.usedId));
            this.shareProduct(e.shareFrom, e.usedId);
        }
        this.getProduct(e.shareFrom, e.usedId);
    },
    onShow: function() {
        var e = this;
        this.startTimer(), a.globalData.callback = function(t) {
            "SUPER_COUPONS_SHARE_OPEND_EVENT" == t.businessType && (console.log("分享打开事件:", t.data.usedId), 
            e.data.item.usedId == t.data.usedId && e.setData(r({}, "item.usedIsShared", 1)));
        };
    },
    onHide: function() {
        this.stopTimer();
    },
    onUnload: function() {
        this.stopTimer();
    },
    startTimer: function() {
        var e = this;
        this.stopTimer(), this.interval = setInterval(function() {
            e.updateCd();
        }, 1e3);
    },
    stopTimer: function() {
        this.interval && (clearInterval(this.interval), this.interval = null);
    },
    updateCd: function() {
        if (this.data.item) {
            var e = this.data.item, t = new Date().getTime() + this.distTime, r = s.timeStringToLong(e.usedCloseTime), a = parseInt((r - t) / 1e3);
            a < 0 && (a = 0);
            var i = parseInt(a / 3600), n = parseInt((a - 3600 * i) / 60), u = a % 60;
            e.timer = "限时：" + (i > 9 ? i : "0" + i) + "小时" + (n > 9 ? n : "0" + n) + "分" + (u > 9 ? u : "0" + u) + "秒神券失效", 
            e.cd = a, this.setData({
                item: e
            });
        }
    },
    detailOnTap: function() {
        wx.redirectTo({
            url: "/pages/act-super-coupon/act-super-coupon"
        });
    },
    onPaySuccess: function() {
        var e;
        this.stopTimer(), this.setData((r(e = {}, "item.usedCloseTime", 0), r(e, "item.cd", -1), 
        e));
    },
    getProduct: function(r, s) {
        var n = this;
        return t(e.default.mark(function t() {
            var u, o;
            return e.default.wrap(function(e) {
                for (;;) switch (e.prev = e.next) {
                  case 0:
                    return e.prev = 0, a.showLoading(), e.next = 4, i.getServerTime();

                  case 4:
                    return u = e.sent, o = new Date().getTime(), n.distTime = u.data.systemTime - o, 
                    e.next = 9, i.superCouponGet({
                        shareFrom: r,
                        usedId: s
                    });

                  case 9:
                    (u = e.sent).data ? (n.setData({
                        item: u.data
                    }), n.updateCd()) : wx.redirectTo({
                        url: "/pages/act-super-coupon/act-super-coupon"
                    }), e.next = 16;
                    break;

                  case 13:
                    e.prev = 13, e.t0 = e.catch(0), console.log(e.t0);

                  case 16:
                    return e.prev = 16, wx.hideLoading(), e.finish(16);

                  case 19:
                  case "end":
                    return e.stop();
                }
            }, t, null, [ [ 0, 13, 16, 19 ] ]);
        }))();
    },
    onShareAppMessage: function(e) {
        if ("button" == e.from) {
            var t = e.target.dataset.prod;
            return {
                title: t.shareTitle,
                path: this.buildSharePath(a.getUsername(), t.usedId),
                imageUrl: t.sharePic
            };
        }
    },
    shareProduct: function(r, a) {
        return t(e.default.mark(function t() {
            return e.default.wrap(function(e) {
                for (;;) switch (e.prev = e.next) {
                  case 0:
                    return e.prev = 0, e.next = 3, i.superCouponShare({
                        shareFrom: r,
                        usedId: a
                    });

                  case 3:
                    e.next = 8;
                    break;

                  case 5:
                    e.prev = 5, e.t0 = e.catch(0), console.log(e.t0);

                  case 8:
                  case "end":
                    return e.stop();
                }
            }, t, null, [ [ 0, 5 ] ]);
        }))();
    },
    buildSharePath: function(e, t) {
        return "/pages/act-super-coupon-share/act-super-coupon-share?usedId=" + t + "&shareFrom=" + e;
    }
});